package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.Sku;
import cn.com.dcsgo.dto.SkuDTO;
import cn.com.dcsgo.dto.StockUpdateDTO;
import cn.com.dcsgo.service.ProdService;
import cn.com.dcsgo.service.SkuService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 商品RPC相关接口
 *
 * @author Dcsgo
 * @since 2025/11/15 10:21
 */
@RestController
@RequestMapping("rpc/prod")
@Tag(name = "商品RPC相关接口")
public class ProdRPCController {
    @Resource
    private SkuService skuService;

    @Resource
    private ProdService prodService;

    @GetMapping("sku/list/ids")
    @Operation(summary = "获取Sku列表", description = "根据SKU ID 列表")
    public List<SkuDTO> getSkuDTOListBySkuIds(@RequestParam("skuIds") Set<Long> skuIds) {
        List<Sku> skuList = skuService.listByIds(skuIds);
        ArrayList<SkuDTO> skuDTOList = new ArrayList<>(skuList.size());
        skuList.forEach(sku -> {
            SkuDTO skuDTO = new SkuDTO();
            BeanUtil.copyProperties(sku, skuDTO);
            skuDTOList.add(skuDTO);
        });
        return skuDTOList;
    }

    @PutMapping("stock")
    public boolean updateStock(@RequestBody StockUpdateDTO stockUpdateDTO) {
        return prodService.updateStock(stockUpdateDTO);
    }
}
